package ar.com.photo_admin.domain

import java.text.SimpleDateFormat
import java.util.Date;
import java.util.Set;

class User extends DomainObject {

	String facebookUid

	String username

	String telephone

	String address

	String password

	String confirmPassword
	
	String oldPassword
	
	String captchaCode

	Date birthday

	String email
	
	String presentationText
	
	byte[] profileImage
	
	String profileImageType
	
	Boolean useFacebookProfileImage = false
	
	Set events

	static hasMany = [events: Event]
	
	void setBirthdayValue(String birthdayValue) {
		def dateParser = new SimpleDateFormat("dd/MM/yyyy")
		birthday = dateParser.parse(birthdayValue)
	 }

	static constraints = {
		username(blank:false, nullable:false, unique:true, matches: '[a-z0-9_.]+', size: 6..16)
		password(blank:false, nullable:true, size: 6..16)
		
		password validator: { password, obj ->
			return obj.confirmPassword == null || obj.confirmPassword == password
		}

		email(email: true, blank:false, nullable:false, unique:true)
		telephone(nullable:true, matches: '[0-9-]+')
		address(nullable:true)
		birthday(nullable:true, max: new Date())
		facebookUid(nullable:true, unique:true)
		presentationText(nullable:true, size: 0..250)
		profileImage(nullable:true, maxSize: 32768 /* 32K */)
		profileImageType(nullable:true)
	}

	static transients = ["confirmPassword", "captchaCode", "oldPassword"]

	static mapping = { tablePerHierarchy false }
}
